package com.powernode.common.config.feign;

import com.powernode.common.FeignCustomerDataDecoder;
import feign.codec.Decoder;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.openfeign.support.HttpMessageConverterCustomizer;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Feign客户端配置类
 * 用于配置Feign客户端的解码器，实现统一的结果处理
 */
@Configuration
public class FeignConfig {
    /**
     * 配置Feign解码器Bean
     * 创建自定义的FeignCustomerDataDecoder，用于统一处理Feign调用的返回结果
     *
     * @param msgConverters HTTP消息转换器工厂，用于处理请求和响应的消息转换
     * @param customizers HTTP消息转换器定制器提供者，用于自定义消息转换行为
     * @return Decoder 自定义的Feign解码器实例
     */
    @Bean
    public Decoder decoder(ObjectFactory<HttpMessageConverters> msgConverters,
                           ObjectProvider<HttpMessageConverterCustomizer> customizers) {
        // 创建自定义的FeignCustomerDataDecoder，包装SpringDecoder
        // 实现对Feign调用返回结果的统一处理，包括错误状态码检查和异常抛出
        return new FeignCustomerDataDecoder(
                new SpringDecoder(msgConverters, customizers)
        );
    }
}
